package com.zd.shopping.activity;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zd.shopping.R;
import com.zd.shopping.apis.DeliverGoodsApi;
import com.zd.shopping.baseadapter.DeliverGoodsAdapter;
import com.zd.shopping.baseadapter.NuclearGoodsAdapter;
import com.zwz.mall.api.OnRequestListener;
import com.zwz.mall.base.BaseActivity;
import com.zwz.mall.utils.BarHeightUtils;

import butterknife.BindView;

/**
 *
 * @author Administrator
 * @date 2019/10/30 0030
 */

public class DeliverGoodsActivity extends BaseActivity
{
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.et_select)
    EditText et_select;
    @BindView(R.id.fake_status_bar)
    View fakeStatusBar;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.enpty)
    RelativeLayout enpty;
    @BindView (R.id.img_select)
    ImageView mImg_select;
    private DeliverGoodsAdapter deliverGoodsAdapter;
  private DeliverGoodsApi deliverGoodsApi=  new DeliverGoodsApi();

    @Override
    public int getLayoutId()
    {
        return R.layout.activity_delivergoods;
    }

    @Override
    public void initPresenter()
    {

    }

    @Override
    public void initView()
    {
        BarHeightUtils.setStatusBarHeight(fakeStatusBar, getStatusBarHeight());
        title.setText ("送货");
        deliverGoodsApi.username=user.getUsername ();
        deliverGoodsApi.searchstr="";
        mRecyclerView.setLayoutManager(new LinearLayoutManager (this));
        deliverGoodsAdapter = new DeliverGoodsAdapter (this, deliverGoodsApi.list);
        mRecyclerView.setAdapter(deliverGoodsAdapter);
        mRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener ()
        {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout)
            {
                //下拉刷新,一般添加调用接口获取数据的方法
//                oprateListApi.page = 0;
                intData();
            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout)
            {
                //上拉加载，一般添加调用接口获取更多数据的方法
                    refreshLayout.setNoMoreData(true);

            }
        });
    }

    private void intData() {
        if (deliverGoodsApi.isLoading())
        {
            return;
        }
        deliverGoodsApi.list.clear();
        deliverGoodsAdapter.notifyDataSetChanged();
        deliverGoodsApi.loadData (new OnRequestListener () {
            @Override
            public void onBefore() {

            }

            @Override
            public void onSuccess() {
                deliverGoodsAdapter.notifyDataSetChanged();
                mRefreshLayout.finishRefresh();
                mRefreshLayout.finishLoadMore();
            }

            @Override
            public void onFailure(Throwable error) {
                mRefreshLayout.finishRefresh();
                mRefreshLayout.finishLoadMore();
            }

            @Override
            public void onNetWorkError() {

            }
        });
    }

    @Override
    public void fillData()
    {
        intData();
    }

    @Override
    public void setListener()
    {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mImg_select.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick(View view) {
                deliverGoodsApi.list.clear ();
                deliverGoodsAdapter.notifyDataSetChanged ();
                deliverGoodsApi.searchstr=et_select.getText ().toString ();
                intData();
            }
        });
    }
}
